﻿using CommonDevelop;
using CommonDevelop.DB;
using CommonEngine;
using CommonFP;
using ManageDB;
using ManageDto;
using ManageRepository;
using System.Collections.Generic;
using System.Linq;

namespace ManageService
{
    /// <summary>
    /// 岗位服务层接口
    /// </summary>
    public interface IPositionService : IBaseService<Base_Position>
    {
        IEnumerable<PositionDto> GetUserPosition(string UserId);
        bool UserRelationPosition(string UserId, params string[] post);
    }

    /// <summary>
    /// 岗位服务层
    /// </summary>
    public class PositionService : BaseService<Base_Position, IPositionRepository>, IPositionService
    {
        /// <summary>
        /// 构造注入
        /// </summary>
        public PositionService(IPositionRepository repository)
            : base(repository)
        {

        }

        public IEnumerable<PositionDto> GetUserPosition(string UserId)
            => FP.Try(() => this.repository_DB.GetPostByUserId(UserId).MapToList<Base_Position, PositionDto>())
            .Run().Match(e => Enumerable.Empty<PositionDto>(), a => a);

        public bool UserRelationPosition(string UserId, params string[] post)
        {
            if (post.IsNullOrEmpty() || UserId.IsEmpty())
                return false;

            return FP.Try(() =>
            {
                List<string> pids = post.ToList();
                var pls = this.repository_DB.GetList(a => pids.Contains(a.PositionId));

                if (pls.IsNullOrEmpty())
                    return 0;

                return this.repository_DB.SavePost(UserId, pls.ToArray());
            }).Run().Match(e => false, a => a > 0);
        }
    }
}
